﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace TerryDong.Data
{
    [global::System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    public sealed class DatabaseAttribute : Attribute
    {
        // See the attribute guidelines at 
        //  http://go.microsoft.com/fwlink/?LinkId=85236
        readonly string positionalString;

        // This is a positional argument
        public DatabaseAttribute(string positionalString)
        {
            this.positionalString = positionalString;

            // TODO: Implement code here
            throw new NotImplementedException();
        }

        public string PositionalString
        {
            get { return positionalString; }
        }

        // This is a named argument
        public int NamedInt { get; set; }
    }

    [global::System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    public sealed class TableAttribute : Attribute
    {
        public TableAttribute()
        {

        }
        public string TableName { get; set; }
        public string DatabaseName { get; set; }
        public string ConnectionString { get; set; }

        public static TableAttribute GetAttributes<T>()
        {
            TableAttribute MyAttribute = (TableAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(TableAttribute));
            return MyAttribute;
        }
        /*
        public static string GetTableName(Type t)
        {
            TableAttribute MyAttribute = (TableAttribute)Attribute.GetCustomAttribute(t, typeof(TableAttribute));
            if (null==MyAttribute)
            {
                throw new ArgumentException("TableAttribute is null");
            }
            return MyAttribute.TableName;
        }

        public static string GetDatabaseName(Type t)
        {
            TableAttribute MyAttribute = (TableAttribute)Attribute.GetCustomAttribute(t, typeof(TableAttribute));
            if (null == MyAttribute)
            {
                throw new ArgumentException("TableAttribute is null");
            }
            return MyAttribute.DatabaseName;
        }

        public static string GetConnectionStringName(Type t)
        {
            TableAttribute MyAttribute = (TableAttribute)Attribute.GetCustomAttribute(t, typeof(TableAttribute));
            if (null == MyAttribute)
            {
                throw new ArgumentException("TableAttribute is null");
            }
            return MyAttribute.ConnectionString;
        }
         * */
    }

    [global::System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
    public sealed class ColumnAttribute : Attribute
    {
        public ColumnAttribute()
        {
            PrimaryKey = false;
            Identity = false;
            AllowNull = true;
        }

        /// <summary>
        /// 是否是主键，默认false
        /// </summary>
        public bool PrimaryKey { get; set; }
        /// <summary>
        /// 是否是自增长列，默认false
        /// </summary>
        public bool Identity { get; set; }

        /// <summary>
        /// 是否允许为空，默认true
        /// </summary>
        public bool AllowNull { get; set; }

        public override bool IsDefaultAttribute()
        {
            if (PrimaryKey==false&&Identity==false&&AllowNull==true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static ColumnAttribute GetAttributes<T>()
        { 
            ColumnAttribute attr = ColumnAttribute.GetCustomAttribute(typeof(T), typeof(ColumnAttribute)) as ColumnAttribute;
            return attr;
        }

        public static bool IsPrimaryKey(PropertyInfo objColumn)
        {
            //System.Reflection.Assembly assembly = objColumn.GetType().Assembly;
            ColumnAttribute attr = ColumnAttribute.GetCustomAttribute(objColumn, typeof(ColumnAttribute)) as ColumnAttribute;
            if (attr.PrimaryKey==true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsIdentity(PropertyInfo objColumn)
        {
            string name=objColumn.GetType().Name;
            ColumnAttribute attr = ColumnAttribute.GetCustomAttribute(objColumn, typeof(ColumnAttribute)) as ColumnAttribute;
            if (attr.Identity == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ContainsPrimaryKey(object objTable)
        {
           PropertyInfo[] properties  = objTable.GetType().GetProperties();
           foreach (PropertyInfo p in properties)
           {
               if (IsPrimaryKey(p))
               {
                   return true;
               }
           }
           return false;
        }

        public static bool ContainsIdentity(object objTable)
        {
            PropertyInfo[] properties = objTable.GetType().GetProperties();
            foreach (PropertyInfo p in properties)
            {
                if (IsIdentity(p))
                {
                    return true;
                }
            }
            return false;
        }
    
    
    }
}
